package com.jiyun.common.model;

import com.baomidou.mybatisplus.annotation.TableField;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import lombok.Value;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.format.annotation.DateTimeFormat;

import javax.persistence.Id;
import javax.persistence.Transient;
import java.lang.annotation.Documented;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @projectName: 2210A-project1
 * @package: com.jiyun.common.model
 * @className: Evaluate
 * @author: 耿嘉军
 * @date: 2023/9/7 19:52
 * @version: 1.0
 */
@Data
@Document(collection = "evaluate")
public class Evaluate {
    @Id
    private String id;
    /**
     *  当前评价的人
     */
    private String playId;
    /**
     * 对课程进行评价
     */
    private String courseId;
    /**
     * 评价内容
     */
    private String eva;
    /**
     * 父id
     */
    private String pid;
    /**
     *创建时间
     */
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    @JsonFormat(pattern = "yyyy-MM-dd")
    private Date createTime;

    /**
     * 子级数据
     */
    @Transient
    @TableField(exist = false)
    private List<Evaluate> children;

    /**
     * 评价人的名字
     */
    @Transient
    @TableField(exist = false)
    private String playName;

    public void addChild(Evaluate child) {

        if (children == null) {
            children = new ArrayList<>();
        }
        children.add(child);
    }

    public void generateComments(List<Evaluate> comments, String parentId) {
        List<Evaluate> childComments = comments.stream()
                .filter(comment -> comment.getPid().equals(parentId))
                .collect(Collectors.toList());

        for (Evaluate childComment : childComments) {
            generateComments(comments, childComment.getId());
            addChild(childComment);
        }
    }
}
